﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BalanceManager.Helpers;
using BalanceManager.Model;
using BalanceManager.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace BalanceManager.Views
{
    public partial class HistoryView : BindablePage
    {
        Stack<TreeNode<HistoryItem>> _nodesStack = new Stack<TreeNode<HistoryItem>>();

        public HistoryView()
        {
            InitializeComponent();
            periodList.ItemsSource = Enum.GetValues(typeof(Period));
            (DataContext as HistoryViewModel).PropertyChanged += OnPropertyChanged;
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (propertyChangedEventArgs.PropertyName == "ItemsTree")
            {
                var viewModel = sender as HistoryViewModel;
                treeListControl.ItemsSource = viewModel.ItemsTree;
            }
        }

        void GoUpCurrentVisibleCategory()
        {
            var viewModel = (DataContext as HistoryViewModel);
            var rootNodes = viewModel.ItemsTree;
            if (_nodesStack.Count == 0)
            {
                treeListControl.ItemsSource = rootNodes;
                //viewModel.SelectedNode = null;
                return;
            }

            // pop current visible item
            _nodesStack.Pop();

            // show upper item if can
            if (_nodesStack.Count == 0)
            {
                treeListControl.ItemsSource = rootNodes;
                //viewModel.SelectedNode = null;
                return;
            }

            var upperNode = _nodesStack.Peek();
            //viewModel.SelectedNode = upperNode;
            treeListControl.ItemsSource = upperNode.Children;
        }

        private void BindablePage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_nodesStack.Count == 0)
            {
                e.Cancel = false;
                base.OnBackKeyPress(e);
            }
            else
            {
                e.Cancel = true;
                GoUpCurrentVisibleCategory();
            }
        }

        void TreeListControl_OnTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var control = sender as LongListSelector;
            var selectedItem = control.SelectedItem as TreeNode<HistoryItem>;
            if (selectedItem == null || (_nodesStack.Count > 0 && selectedItem == _nodesStack.Peek())) return;

            control.ItemsSource = selectedItem.Children;
            _nodesStack.Push(selectedItem);
            //(DataContext as ItemsViewModel).SelectedNode = selectedItem;    
        }
    }
}